package omq.api;

import java.io.IOException;

import omq.utils.OMQConversions;

/**
 * The OMQMessage object represent a message from OnlineMQ.
 * It contains the message content in a few formats, it's type, and in some cases - the message-Id.
 * The encoding is only relevant to the way the messages are sent between Java and OnlineMQ, 
 * we always use Base64 to transfer the message.
 * The message itself can be retrieved from OMQMessage in the following formats:
 * - Base64 as String
 * - byte-array as byte[]
 * - UTF-8 String
 * 
 * @author hagaii
 *
 */
public class OMQMessage 
{
	byte[] msgByteArray;
	String msgBase64;
	/*
	 * a message doesn't have an encoding, the encoding is only relevant to the way
	 * the message is send to and from OnlineMQ
	 */ 
//	OMQBodyEncoding msgEncoding;
	OMQBodyType msgType;
	
	String msgSender = ""; 
	String msgID = null; // usually used when receiving a message
	
	/**
	 * This constructor is used to initialize the OMQMessage content with a Java object 
	 * @param msg -
	 * @param type - omq
	 * @throws IOException
	 */
	public OMQMessage(Object msgObject) throws IOException
	{
		msgBase64 = OMQConversions.Object2Base64(msgObject);
		msgType = new OMQBodyTypeOther();
		msgByteArray = OMQConversions.Base642ByteArray(msgBase64);
	}

	/**
	 * This constructor is used to initialize the Message object with a byte-array (byte[]) 
	 * @param ba
	 * @throws IOException
	 */
	public OMQMessage(byte[] ba) throws IOException
	{
		msgByteArray = ba;
		msgBase64 = OMQConversions.ByteArray2Base64(ba);
		msgType = new OMQBodyTypeOther();
	}
	
	/**
	 * This constructor is used to initialize the Message object with data from a receive_message action against the OMQ servers
	 * the message body, that is a Java-String type is in most cases a base64 String, and you should also initialize the body-type
	 * and the message-id from the object received from the receive_message.
	 * In java, we force the send_message and receive_message to encode the message with base64.
	 * @param bodyBase64 - the message content as String from receive_message 
	 * @param type
	 * @param id
	 * @param sender
	 * @throws IOException
	 */
	public OMQMessage(String bodyBase64, OMQBodyType type, String id, String sender) throws IOException
	{
		msgBase64 = bodyBase64;
		msgByteArray = OMQConversions.Base642ByteArray(bodyBase64);
		msgSender = sender;
		msgType = type;
		msgID = id;
	}

	/**
	 * get message content as object
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public Object getMsgBodyAsObject() throws IOException, ClassNotFoundException
	{
		return OMQConversions.Base642Object(msgBase64);
	}

	/**
	 * get message content as base64 String
	 * @return
	 */
	public String getMsgBodyasBase64() {
		return msgBase64;
	}

	/**
	 * get message content as a byte-array
	 * @return
	 */
	public byte[] getMsgBodyasByteArray() {
		return msgByteArray;
	}
	
	/**
	 * get message type (used when receiving a message from receive_message)
	 * @return
	 */
	public OMQBodyType getMsgType() {
		return msgType;
	}

	/**
	 * get message id (used when receiving a message from receive_message)
	 * @return
	 */
	public String getMsgID() {
		return msgID;
	}

	public void setMsgID(String msgID) {
		this.msgID = msgID;
	}

	public String getSender() {
		return msgSender;
	}


}